/*************************************************************************
	> File Name: clienthandler.cpp
	> Author: xuwenlong
	> Mail: myxuan475@126.com 
	> Created Time: 2018年10月09日 星期二 09时50分02秒
 ************************************************************************/
#include "clienthandler.h"
#include <transport/remote/client/sclient.h>
#include "dasclient.h"
#include <utils/config.h>

ClientHandler::ClientHandler(void *interface):
    ProtocolHandler(ProtocolHandler::HAS_TIMER,(TransportInterface*)interface) {

}

ClientHandler::~ClientHandler()
{
}

DAS_RESULT ClientHandler::OnRequest(
        int msgId,
        const std::string &method,
        const Json::Value &params,
        Json::Value &result)
{
    if (method == "telnetStart") {
        DAS_RESULT ret = instance()->Start(params);
        result = (ret==DAS_SUCCESS);
        return DAS_SUCCESS;
    }
    else if (method == "telnetStop") {
        instance()->Stop();
        result = true;
        return DAS_SUCCESS;
    }
    else if (method == "startDaemon") {
        int port = params["port"].asInt();
//        Sclient *client = static_cast<Sclient*>(m_pTransport);
//        client->startServer(port);

        std::string strId = utils::GetConfigFileStringValue("Config.ini",GetSection(),"id","");

        result["company"]=utils::GetConfigFileStringValue("Config.ini","BASE","company","");
        result["gateway_id"]= strId;
        result["version"] = DAS_VERSION;
        return DAS_SUCCESS;
    }
    else {
        return ProtocolHandler::OnRequest(
                    msgId,
                    method,
                    params,
                    result);
    }
    return DAS_INVALID;
}

Remoted *ClientHandler::instance()
{
    static Remoted *telnet = NULL;

    if (!telnet)
        telnet = new Remoted();
    return telnet;
}
